package io;

import java.io.*;
import java.util.Scanner;

public class IODemo12 {
    // 拷贝文件
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("请输入要拷贝的文件路径:");
        String srcPath = in.next();
        System.out.println("请输入要拷贝到哪个地方:");
        String destPath = in.next();

        // 判断源文件是否存在
        File srcFile = new File(srcPath);
        if (!srcFile.isFile()) {
            System.out.println("要拷贝的文件不存在");
            return;
        }
        // 判断要拷贝的目的地是否已经存在该文件
        File destFile = new File(destPath);
        if (destFile.isFile()) {
            System.out.println("路径有误!");
            return;
        }

        // 拷贝操作
        try(InputStream inputStream = new FileInputStream(srcFile);
            OutputStream outputStream = new FileOutputStream(destFile)) {
            while (true) {
                int b = inputStream.read();
                if (b == -1) {
                    break;
                }
                outputStream.write(b);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
